package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {
    @Autowired
    private SaleListGoodsDao saleListGoodsDao;
    @Autowired
    private SaleListDao saleListDao;
    @Autowired
    private GoodsDao goodsDao;


    private Gson gson = new Gson();

    /**
     * 保存销售单
     *
     * @param saleList
     * @param saleListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(SaleList saleList, String saleListGoodsStr) {
        if (StringUtil.isEmpty(saleListGoodsStr)) {
            return new ServiceVO(400, "销售的商品不能为空");
        }
        List<SaleListGoods> list = gson.fromJson(saleListGoodsStr, new TypeToken<List<SaleListGoods>>() {
        }.getType());
        for (SaleListGoods saleListGoods : list) {
            Goods good = goodsDao.getById(saleListGoods.getGoodsId());
            Integer inventoryQuantity = good.getInventoryQuantity();
            if (saleListGoods.getGoodsNum() > inventoryQuantity) {
                return new ServiceVO(400, "销售的商品数量不能大于库存");
            }
            //更新商品库存
            goodsDao.updateQuantity(saleListGoods.getGoodsId(), inventoryQuantity - saleListGoods.getGoodsNum());
            //添加销售出的商品
            saleListGoodsDao.save(saleListGoods);
        }
        //添加销售单
        saleListDao.save(saleList);
        return new ServiceVO(100,"请求成功");
    }

    @Override
    public List<SaleList> list(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return saleListDao.selectList(saleNumber,customerId,state,start,end);
    }

    @Override
    public List<SaleListGoods> goodsList(Integer saleListId) {
        return saleListGoodsDao.selectByListId(saleListId);
    }

    @Override
    public void delete(Integer saleListId) {
        List<SaleListGoods> saleListGoods = saleListGoodsDao.selectByListId(saleListId);
        if(null != saleListGoods && saleListGoods.size() > 0){
            saleListGoodsDao.deleteByListId(saleListId);
        }
        saleListDao.delete(saleListId);
    }

    @Override
    public void updateState(Integer saleListId) {
        SaleList saleList = saleListDao.getById(saleListId);
        if(saleList.getState()==1){
            saleListDao.updateState(saleListId,2);
        }else{
            saleListDao.updateState(saleListId,1);
        }
    }

    @Override
    public List<SaleCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return saleListGoodsDao.selectCount(start,end,goodsTypeId,codeOrName);
    }

    @Override
    public List<SaleData> getSaleDateByDay(String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<SaleData> saleDateByDay = saleListGoodsDao.getSaleDateByDay(start, end);
        HashSet<String> distinct = new HashSet<>();
        Iterator<SaleData> iterator = saleDateByDay.iterator();
        while(iterator.hasNext()){
            SaleData next = iterator.next();
            if(distinct.contains(next.getDate())){
                iterator.remove();
            }else{
            distinct.add(next.getDate());
            }
        }
        return saleDateByDay;

    }

    @Override
    public List<SaleData> getSaleDateByMonth(String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<SaleData> saleDateByMonth = saleListGoodsDao.getSaleDateByMonth(start, end);
        HashSet<String> distinct = new HashSet<>();
        Iterator<SaleData> iterator = saleDateByMonth.iterator();
        while (iterator.hasNext()){
            SaleData next = iterator.next();
            if(distinct.contains(next.getDate())){
                iterator.remove();
            }else{
                distinct.add(next.getDate());
            }
        }
        return saleDateByMonth;
    }
}
